package com.cloud.bc.service.impl;

import com.cloud.bc.model.entity.BcArticles;
import com.cloud.bc.mapper.BcArticlesMapper;
import com.cloud.bc.service.BcArticlesService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.List;

/**
 * 文章
 *
 * @author douhaichao code generator
 * @date 2021-01-01 19:24:20
 */
@Service
@AllArgsConstructor
public class BcArticlesServiceImpl extends ServiceImpl<BcArticlesMapper, BcArticles> implements BcArticlesService {

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */
    @Override
    public BcArticles selectOne(BcArticles req) {
        QueryWrapper queryWrapper = new QueryWrapper<BcArticles>();
        queryWrapper.setEntity(req);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 根据条件查询所有
     */
    @Override
    public List<BcArticles> selectAll(BcArticles req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据对象查询所有分页数据(精确查询)     *      * @return
     */
    @Override
    public IPage<BcArticles> selectPage(Page page, BcArticles req) {
        QueryWrapper<BcArticles> bcArticlesQueryWrapper = new QueryWrapper<BcArticles>();
        bcArticlesQueryWrapper.setEntity(req);
        bcArticlesQueryWrapper.orderByDesc("id");
        IPage<BcArticles> list = baseMapper.selectPage(page, bcArticlesQueryWrapper);
        return list;
    }

    /**
     * 根据对象查询所有分页数据(模糊查询)     *      * @return
     */
    @Override
    public IPage<BcArticles> selectPageByLike(Page page, BcArticles req) {
        QueryWrapper queryWrapper = new QueryWrapper();

        if (null != req.getId()) {
            queryWrapper.eq("id", req.getId());
        }

        if (null != req.getCategoryId()) {
            queryWrapper.eq("category_id", req.getCategoryId());
        }

        if (!StrUtil.isEmpty(req.getTitle())) {
            queryWrapper.like("title", req.getTitle());
        }

        if (!StrUtil.isEmpty(req.getCover())) {
            queryWrapper.like("cover", req.getCover());
        }

        if (!StrUtil.isEmpty(req.getSummary())) {
            queryWrapper.like("summary", req.getSummary());
        }

        if (!StrUtil.isEmpty(req.getContent())) {
            queryWrapper.like("content", req.getContent());
        }

        if (!StrUtil.isEmpty(req.getTag())) {
            queryWrapper.like("tag", req.getTag());
        }

        if (null != req.getUserId()) {
            queryWrapper.eq("user_id", req.getUserId());
        }

        if (!StrUtil.isEmpty(req.getAuthor())) {
            queryWrapper.like("author", req.getAuthor());
        }

        if (!StrUtil.isEmpty(req.getSource())) {
            queryWrapper.like("source", req.getSource());
        }

        if (!StrUtil.isEmpty(req.getUrl())) {
            queryWrapper.like("url", req.getUrl());
        }

        if (!StrUtil.isEmpty(req.getSeoKeywords())) {
            queryWrapper.like("seo_keywords", req.getSeoKeywords());
        }

        if (!StrUtil.isEmpty(req.getSeoDescription())) {
            queryWrapper.like("seo_description", req.getSeoDescription());
        }

        if (null != req.getBrowse()) {
            queryWrapper.eq("browse", req.getBrowse());
        }

        if (null != req.getPraise()) {
            queryWrapper.eq("praise", req.getPraise());
        }

        if (null != req.getDisplay()) {
            queryWrapper.eq("display", req.getDisplay());
        }

        if (null != req.getCreateTime()) {
            queryWrapper.eq("create_time", req.getCreateTime());
        }

        if (null != req.getMotifyTime()) {
            queryWrapper.eq("motify_time", req.getMotifyTime());
        }

        if (null != req.getIsDel()) {
            queryWrapper.eq("is_del", req.getIsDel());
        }

        if (!StrUtil.isEmpty(req.getCategoryName())) {
            queryWrapper.like("category_name", req.getCategoryName());
        }
        IPage<BcArticles> list = baseMapper.selectPage(page, queryWrapper);
        return list;
    }

    /**
     * 根据条件统计数据量     *      * @return
     */
    @Override
    public int count(BcArticles req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectCount(queryWrapper);
    }
}
